home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / ISp Sample / Source / AppleEventHandler.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  3.1 KB  |  150 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        AppleEventHandler.c
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1999 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                xxx put dri here xxx
  13.  
  14.         Other Contact:        xxx put other contact here xxx
  15.  
  16.         Technology:            xxx put technology here xxx
  17.  
  18.     Writers:
  19.  
  20.         (BWS)    Brent Schorsch
  21.  
  22.     Change History (most recent first):
  23.  
  24.        <SP1>      7/1/99    BWS        first checked in
  25. */
  26.  
  27. //•    ————————————————————————————————————————    Includes
  28.  
  29. #include <Errors.h>
  30.  
  31. #include "AppleEventHandler.h"
  32.  
  33. //•    ————————————————————————————————————————    Private Definitions
  34. //•    ————————————————————————————————————————    Private Types
  35. //•    ————————————————————————————————————————    Private Variables
  36.  
  37. static AEEventHandlerUPP    gAppleEventHandler;
  38.  
  39. static Boolean    *gQuitFlag;
  40.  
  41. //•    ————————————————————————————————————————    Private Functions
  42.  
  43. static OSErr AppleEventHandler(const AppleEvent *inEvent, AppleEvent *outReply, UInt32 inRefCon);
  44.  
  45. //•    ————————————————————————————————————————    Public Variables
  46.  
  47. //•    ————————————————————    AppleEventsInit
  48.  
  49. void
  50. AppleEventsInit(void)
  51. {
  52.     //•    Install our AppleEvent dispatch routine
  53.     gAppleEventHandler = NewAEEventHandlerProc(AppleEventHandler);
  54.     AEInstallEventHandler(typeWildCard, typeWildCard, gAppleEventHandler, 0L, false);
  55. }
  56.  
  57. //•    ————————————————————    AppleEventsShutDown
  58.  
  59. void
  60. AppleEventsShutDown(void)
  61. {
  62.     AERemoveEventHandler(typeWildCard, typeWildCard, gAppleEventHandler, false);
  63.     DisposeRoutineDescriptor(gAppleEventHandler);
  64. }
  65.  
  66. #pragma mark -
  67.  
  68. //•    ————————————————————    AppleEventsGotRequiredParams
  69.  
  70. Boolean
  71. AppleEventsGotRequiredParams(const AppleEvent *inEvent)
  72. {
  73. DescType    returnedType;
  74. Size        actualSize;
  75. OSErr        theErr;
  76.  
  77.     theErr = AEGetAttributePtr(inEvent, keyMissedKeywordAttr, typeWildCard, &returnedType, nil, 0,  &actualSize);
  78.  
  79.     return (errAEDescNotFound == theErr);
  80.  
  81. }
  82.  
  83.  
  84. //•    ————————————————————    AppleEventsRegisterQuitFlag
  85.  
  86. void
  87. AppleEventsRegisterQuitFlag(Boolean *inFlag)
  88. {
  89.     if (nil != inFlag)
  90.         gQuitFlag = inFlag;
  91. }
  92.  
  93. //•    ————————————————————    AppleEventHandler
  94.  
  95. static OSErr
  96. AppleEventHandler(const AppleEvent *inEvent, AppleEvent *outReply, UInt32 inRefCon)
  97. {
  98. #pragma unused (outReply, inRefCon)
  99.  
  100. OSErr        theErr;
  101. DescType    actualType;
  102. Size        actualSize;
  103. DescType    eventClass, eventID;
  104.  
  105.     theErr = AEGetAttributePtr(inEvent, keyEventClassAttr, typeType, &actualType, &eventClass, sizeof (eventClass), &actualSize);
  106.     if (noErr != theErr)
  107.         return (theErr);
  108.  
  109.     theErr = AEGetAttributePtr(inEvent, keyEventIDAttr, typeType, &actualType, &eventID, sizeof (eventID), &actualSize);
  110.     if (noErr != theErr)
  111.         return (theErr);
  112.  
  113.     if (eventClass == kCoreEventClass)
  114.     {
  115.         switch (eventID)
  116.         {
  117.             case kAEOpenApplication:
  118.                 return (errAEEventNotHandled);
  119.                 break;
  120.                 
  121.             case kAEOpenDocuments:
  122.                 return (errAEEventNotHandled);
  123.                 break;
  124.                     
  125.             case kAEPrintDocuments:
  126.                 return (errAEEventNotHandled);
  127.                 break;
  128.                 
  129.             case kAEQuitApplication:
  130.                 if (AppleEventsGotRequiredParams(inEvent))
  131.                 {
  132.                     if (nil != gQuitFlag)
  133.                         *gQuitFlag ^= 1;
  134.  
  135.                     return (noErr);
  136.                 }
  137.                 else
  138.                 {
  139.                     return (errAEParamMissed);
  140.                 }
  141.                 break;
  142.                 
  143.             default:
  144.                 return (errAEHandlerNotFound);
  145.         }        
  146.     }
  147.         
  148.     return (noErr);
  149. }
  150.